package com.ktjy.web.interceptors;

import com.ktjy.entity.SysUser;
import com.ktjy.utils.Constants;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;
import org.springframework.web.servlet.HandlerInterceptor;

public class AuthInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request,
                             HttpServletResponse response,
                             Object handler) throws Exception {
        //从session中获取登录的用户信息
        HttpSession session = request.getSession();
        SysUser sysUser = (SysUser)session.getAttribute(Constants.SESSION_USER);
        //判断又没有从session获取到用户信息
        if(sysUser==null){//没有登录
            //跳转到没有权限访问的页面401.jsp
            request.getRequestDispatcher("/WEB-INF/jsp/401.jsp")
                    .forward(request,response);
            //走不了
            return false;
        }
        //放行
        return true;
    }
}
